import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { FundItem, StockItem } from '@/data/InvestmentDB'

// 定义盈亏数据结构
interface ProfitData {
  todayProfit: number
  holdingProfit: number
  totalHoldingAmount: number
  totalInvestmentAmount: number
}

export const useProfitStore = defineStore('profit', () => {
  // 基金盈亏数据
  const fundProfit = ref<ProfitData>({
    todayProfit: 0,
    holdingProfit: 0,
    totalHoldingAmount: 0,
    totalInvestmentAmount: 0,
  })

  // 股票盈亏数据
  const stockProfit = ref<ProfitData>({
    todayProfit: 0,
    holdingProfit: 0,
    totalHoldingAmount: 0,
    totalInvestmentAmount: 0,
  })

  // 更新基金盈亏数据
  const updateFundProfit = (funds: FundItem[]) => {
    let todayProfitTotal = 0
    let holdingProfitTotal = 0
    let totalHoldingAmount = 0
    let totalInvestmentAmount = 0

    funds.forEach((item) => {
      // 使用String()函数确保类型统一，避免转换问题
      // 安全地将值转换为数字，处理null、undefined和非数字字符串
      const todayProfit = isNaN(parseFloat(String(item.todayProfit)))
        ? 0
        : parseFloat(String(item.todayProfit))
      const profit = isNaN(parseFloat(String(item.profit))) ? 0 : parseFloat(String(item.profit))
      const holdingValue = isNaN(parseFloat(String(item.holdingValue)))
        ? 0
        : parseFloat(String(item.holdingValue))
      const investment = isNaN(parseFloat(String(item.investment)))
        ? 0
        : parseFloat(String(item.investment))

      todayProfitTotal = parseFloat((todayProfitTotal + todayProfit).toFixed(2))
      holdingProfitTotal = parseFloat((holdingProfitTotal + profit).toFixed(2))
      totalHoldingAmount = parseFloat((totalHoldingAmount + holdingValue).toFixed(2))
      totalInvestmentAmount = parseFloat((totalInvestmentAmount + investment).toFixed(2))
    })

    fundProfit.value = {
      todayProfit: todayProfitTotal,
      holdingProfit: holdingProfitTotal,
      totalHoldingAmount: totalHoldingAmount,
      totalInvestmentAmount: totalInvestmentAmount,
    }
  }

  // 更新股票盈亏数据
  const updateStockProfit = (stocks: StockItem[]) => {
    let todayProfitTotal = 0
    let holdingProfitTotal = 0
    let totalHoldingAmount = 0
    let totalInvestmentAmount = 0

    stocks.forEach((item) => {
      // 安全地将值转换为数字，处理null、undefined和非数字字符串
      const todayProfit = isNaN(parseFloat(String(item.todayProfit)))
        ? 0
        : parseFloat(String(item.todayProfit))
      const profit = isNaN(parseFloat(String(item.profit))) ? 0 : parseFloat(String(item.profit))
      const holdingValue = isNaN(parseFloat(String(item.holdingValue)))
        ? 0
        : parseFloat(String(item.holdingValue))
      const investment = isNaN(parseFloat(String(item.investment)))
        ? 0
        : parseFloat(String(item.investment))

      todayProfitTotal = parseFloat((todayProfitTotal + todayProfit).toFixed(2))
      holdingProfitTotal = parseFloat((holdingProfitTotal + profit).toFixed(2))
      totalHoldingAmount = parseFloat((totalHoldingAmount + holdingValue).toFixed(2))
      totalInvestmentAmount = parseFloat((totalInvestmentAmount + investment).toFixed(2))
    })

    stockProfit.value = {
      todayProfit: todayProfitTotal,
      holdingProfit: holdingProfitTotal,
      totalHoldingAmount: totalHoldingAmount,
      totalInvestmentAmount: totalInvestmentAmount,
    }
  }

  return {
    fundProfit,
    stockProfit,
    updateFundProfit,
    updateStockProfit,
  }
})
